<?php
namespace app\api\controller\v2\controller\snapshot;

use app\api\controller\v2\model\snapshot\Snapshot as Sn;
use think\Controller;

use app\api\controller\Api;
use think\Db;
use think\Image;
use think\Request;
use JPush\Client as JPush;
use think\Response;
use think\response\Redirect;


/**
 *
 */
class Snapshot extends api
{

    //返回随手拍主页面
    public function get_snapshot_img(){
        $arr = (new Sn())->getSnapShotImg();
        return $this->returnmsg(200,"随手拍", $arr);
    }

    //发随手拍
    public function snapshot_posts($user_id,$circle_id,$latitude,$longitude,$address,$content){
        Db::startTrans();
        $time = date('Y-m-d H:i:s');
        if (!empty($address)) {
            $data = ['circle_id'=>$circle_id,'user_id'=>$user_id,'content'=>$content,'post_latitude'=>$latitude,'post_longitude'=>$longitude,'user_address'=>$address,'send_time'=>$time];
            $post_id = Db::table('ym_snapshot_posts')->insertGetId($data);

            Db::query("insert into ym_posts_upvoteCount values(null,'$post_id',0)");

        }else{
            $data = ['circle_id'=>$circle_id,'user_id'=>$user_id,'content'=>$content,'post_latitude'=>null,'post_longitude'=>null,'user_address'=>null,'send_time'=>$time];
            $post_id = Db::table('ym_snapshot_posts')->insertGetId($data);

            Db::query("insert into ym_posts_upvoteCount values(null,'$post_id',0)");
        }
        $files = Request::instance()->file('img');
//        foreach($files as $file){
//            $info = $file->move(ROOT_PATH . 'public' . DS. 'uploads'.DS.'snapshot_img');
//            if ($info) {
//                $path = '/uploads/snapshot_img/' . $info->getSaveName();
//                $path = str_replace('\\', '/', $path);
//                // echo $path."<br />";
//                Db::query("insert into ym_snapshot_img values(null,'$post_id','$path')");
//            }
//        }
        $info = $files->move(ROOT_PATH . 'public' . DS. 'uploads'.DS.'snapshot_img');
        if ($info) {
            $path = '/uploads/snapshot_img/' . $info->getSaveName();
            $path = str_replace('\\', '/', $path);
            // echo $path."<br />";
            Db::query("insert into ym_snapshot_img values(null,'$post_id','$path')");
        }
        Db::commit();
        return $this->returnmsg(200,"发表话题成功");
    }


    //随手拍,删除文章11-10
    public function delete_snapshot_post($post_id){
        Db::query("delete from ym_snapshot_posts where post_id = '$post_id'");
        $result = Db::query("select *from  ym_snapshot_posts where post_id = '$post_id'");
        if (empty($result)) {
            Db::query("delete from ym_snapshot_img where post_id = '$post_id'");
            Db::query("delete from ym_snapshot_posts_upvote where post_id = '$post_id'");
            Db::query("delete from ym_posts_upvoteCount where post_id = '$post_id'");
            Db::query("delete from ym_snapshot_cmmt where post_id = '$post_id'");
            Db::query("delete from ym_snapshot_cmmt_upvote where post_id = '$post_id'");
            Db::query("delete from ym_cmmt_upvoteCount where post_id = '$post_id'");
            return $this->returnmsg(200,"删除成功");
        }else{
            return $this->returnmsg(400,"删除失败");
        }
    }

    //随手拍点赞
    public function posts_upvote($post_id,$user_id){

        $time = date('Y-m-d H:i:s');
        $post = Db::query("select user_id from ym_snapshot_posts where post_id = '$post_id'");
        $be_user_id = $post[0]['user_id'];
        $img = Db::query("select img_url from ym_snapshot_img where post_id = '$post_id' limit 1");
        $img_url = $img[0]['img_url'];

        $result = Db::query("select upvote from ym_snapshot_posts_upvote where post_id = '$post_id'");
        $array = Db::query(" select count(*)from ym_snapshot_posts_upvote where post_id = '$post_id'");
        $count = $array[0]['count(*)'];

        if (empty($result)) {
            Db::query("insert into ym_snapshot_posts_upvote values(null,'$post_id','$user_id',1)");
            Db::query("update ym_posts_upvoteCount set upvoteCount = 1 where post_id = '$post_id'");

            //推送消息
            $message_data = ['be_user_id'=>$be_user_id,'user_id'=>$user_id,'message_type'=>2,'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            $upvote_data = ['m_id'=>$m_id,'module_type'=>0,'id'=>$post_id,'img_url'=>$img_url];
            Db::table('ym_message_upvote')->insertGetId($upvote_data);

            $user_name = Db::query("select user_name from user_info where user_id = '$user_id'");
            $name = $user_name[0]['user_name'];
            // $message = '关注了你';
            $be_user_id = strval($be_user_id);
            $this->push($be_user_id,$name.'赞了你的话题帖子');
            return $this->returnmsg(200,"点赞成功");
        }else{
            Db::query("insert into ym_snapshot_posts_upvote values(null,'$post_id','$user_id',1)");
            Db::query("update ym_posts_upvoteCount set upvoteCount = '$count' + 1 where post_id = '$post_id'");

            //推送消息
            $message_data = ['be_user_id'=>$be_user_id,'user_id'=>$user_id,'message_type'=>2,'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            $upvote_data = ['m_id'=>$m_id,'module_type'=>0,'id'=>$post_id,'img_url'=>$img_url];
            Db::table('ym_message_upvote')->insertGetId($upvote_data);

            $user_name = Db::query("select user_name from user_info where user_id = '$user_id'");
            $name = $user_name[0]['user_name'];

            $be_user_id = strval($be_user_id);
            $this->push($be_user_id,$name.'赞了你的话题帖子');
            return $this->returnmsg(200,"点赞成功");
        }
    }

//  显示随手拍评论
    public function show_post_cmmt($user_id,$post_id,$pageno){
        $result = Sn::getShowPostCmmt($user_id,$post_id,$pageno);
        return $this->returnmsg(200,'随手拍评论',$result);
    }

    //随手拍，评论(一级)
    public function posts_cmmt($post_id,$user_id,$super_user_id,$super_cmmt_id,$cmmt_content){
        $time = date('Y-m-d H:i:s');
        $post = Db::query("select user_id from ym_snapshot_posts where post_id = '$post_id'");
        $be_user_id = $post[0]['user_id'];
        $img = Db::query("select img_url from ym_snapshot_img where post_id = '$post_id' limit 1");
        $img_url = $img[0]['img_url'];
        // base64_decode($cmmt_content);
        if (!empty($super_user_id) && !empty($super_cmmt_id)) {

            $data = ['post_id'=>$post_id,'user_id'=>$user_id,'super_user_id'=>$super_user_id,'super_cmmt_id'=>$super_cmmt_id,'cmmt_content'=>$cmmt_content,'create_time'=>$time];
            $cmmt_id = Db::table('ym_snapshot_cmmt')->insertGetId($data);

            Db::query("insert into ym_cmmt_upvoteCount values(null,'$cmmt_id','$post_id',0)");

            $message_data = ['be_user_id'=>$super_user_id,'user_id'=>$user_id,'message_type'=>4,'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            $reply_data = ['m_id'=>$m_id,'module_type'=>0,'id'=>$post_id,'cmmt_id'=>$cmmt_id,'cmmt_content'=>$cmmt_content,'img_url'=>$img_url];
            $m_reply_id = Db::table('ym_message_reply')->insertGetId($reply_data);

            $user_name = Db::query("select user_name from user_info where user_id = '$user_id'");
            $name = $user_name[0]['user_name'];
            // $message = '关注了你';
            $be_user_id = strval($be_user_id);
            $this->push($be_user_id,$name.'评论了你的话题帖子,'.$cmmt_content);
            return $this->returnmsg(200,'回复成功');
        }else{
            $data = ['post_id'=>$post_id,'user_id'=>$user_id,'cmmt_content'=>$cmmt_content,'create_time'=>$time];
            $cmmt_id = Db::table('ym_snapshot_cmmt')->insertGetId($data);

            Db::query("insert into ym_cmmt_upvoteCount values(null,'$cmmt_id','$post_id',0)");

            //推送消息
            $message_data = ['be_user_id'=>$be_user_id,'user_id'=>$user_id,'message_type'=>3,'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            $cmmt_data = ['m_id'=>$m_id,'module_type'=>0,'id'=>$post_id,'content'=>$cmmt_content,'img_url'=>$img_url];
            $m_cmmt_id = Db::table('ym_message_cmmt')->insertGetId($cmmt_data);

            $user_name = Db::query("select user_name from user_info where user_id = '$user_id'");
            $name = $user_name[0]['user_name'];
            // $message = '关注了你';
            $be_user_id = strval($be_user_id);
            $this->push($be_user_id,$name.'评论了你的话题帖子,'.$cmmt_content);
            return $this->returnmsg(200,'发表评论成功',$cmmt_content);
        }
    }

    //随手拍删除评论
    public function delete_snapshot_post_cmmt($post_id,$cmmt_id){
        Db::query("delete from ym_snapshot_cmmt where post_id = '$post_id' and cmmt_id = '$cmmt_id'");
        $result = Db::query("select *from ym_snapshot_cmmt where post_id = '$post_id' and cmmt_id = '$cmmt_id'");
        if (empty($result)) {
            Db::query("delete from ym_snapshot_cmmt_upvote where post_id = '$post_id' and cmmt_id = '$cmmt_id'");
            Db::query("delete from ym_cmmt_upvoteCount where post_id = '$post_id' and cmmt_id = '$cmmt_id'");
            return $this->returnmsg(200,"删除成功");
        }else{
            return $this->returnmsg(400,"删除失败");
        }
    }


    //随手拍评论点赞
    public function cmmt_upvote($post_id,$cmmt_id,$user_id){

        $time = date('Y-m-d H:i:s');
        $cmmt = Db::query("select user_id from ym_snapshot_cmmt where post_id = '$post_id' and cmmt_id = '$cmmt_id'");
        $be_user_id = $cmmt[0]['user_id'];
        $img = Db::query("select img_url from ym_snapshot_img where post_id = '$post_id' limit 1");
        $img_url = $img[0]['img_url'];

        $result = Db::query("select cmmt_upvote from ym_snapshot_cmmt_upvote where post_id = '$post_id' and cmmt_id = '$cmmt_id'");
        $array = Db::query(" select cmmt_upvoteCount from ym_cmmt_upvotecount where post_id = '$post_id' and cmmt_id = '$cmmt_id'");
        $count = $array[0]['cmmt_upvoteCount'];
        if (empty($result)) {
            Db::query("insert into ym_snapshot_cmmt_upvote values(null,'$cmmt_id','$post_id','$user_id',1)");
            Db::query("update ym_cmmt_upvotecount set cmmt_upvoteCount = 1 where post_id = '$post_id' and cmmt_id = '$cmmt_id'");

            //推送消息
            $message_data = ['be_user_id'=>$be_user_id,'user_id'=>$user_id,'message_type'=>2,'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            $upvote_data = ['m_id'=>$m_id,'module_type'=>0,'id'=>$post_id,'cmmt_id'=>$cmmt_id,'img_url'=>$img_url];
            $m_upvote_id = Db::table('ym_message_upvote')->insertGetId($upvote_data);

            $user_name = Db::query("select user_name from user_info where user_id = '$user_id'");
            $name = $user_name[0]['user_name'];
            // $message = '关注了你';
            $be_user_id = strval($be_user_id);
            $this->push($be_user_id,$name.'赞了你的评论');
            return $this->returnmsg(200,"点赞成功");
        }else{
            Db::query("insert into ym_snapshot_cmmt_upvote values(null,'$cmmt_id','$post_id','$user_id',1)");
            Db::query("update ym_cmmt_upvotecount set cmmt_upvoteCount = '$count' + 1 where post_id = '$post_id' and cmmt_id = '$cmmt_id'");

            //推送消息
            $message_data = ['be_user_id'=>$be_user_id,'user_id'=>$user_id,'message_type'=>2,'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            $upvote_data = ['m_id'=>$m_id,'module_type'=>0,'id'=>$post_id,'cmmt_id'=>$cmmt_id,'img_url'=>$img_url];
            $m_upvote_id = Db::table('ym_message_upvote')->insertGetId($upvote_data);

            $user_name = Db::query("select user_name from user_info where user_id = '$user_id'");
            $name = $user_name[0]['user_name'];

            $be_user_id = strval($be_user_id);
            $this->push($be_user_id,$name.'赞了你的评论');
            return $this->returnmsg(200,"点赞成功");
        }
    }

//转发随手拍
    public function posts_forward($post_id,$user_id,$super_user_id){
      $arr = Sn::postsForward($post_id,$user_id,$super_user_id);
        return $this->returnmsg(200,"转发成功",$arr);
    }

    //随手拍热点话题
    public function get_hot_post($user_id,$circle_id,$pageno,$type){

//        $posts = Sn::getHotPost($user_id,$circle_id,$pageno,$type);
//
//        return $this->returnmsg(200,"posts",$posts);


        $startno = $pageno * 3;
        if ($type == 'hot') {

            $posts = Db::query("select distinct b.user_id,b.user_name,b.user_icon,c.circle_title,c.circle_subtitle,c.circle_desc,c.circle_big_img,a.post_id,a.content,a.post_latitude,a.post_longitude,a.user_address,a.send_time,e.upvoteCount from ym_snapshot_posts as a left join user_info as b on a.user_id=b.user_id left join ym_snapshot_circle as c on a.circle_id=c.circle_id join ym_posts_upvoteCount as e on a.post_id = e.post_id where  c.circle_id = '$circle_id' ORDER BY e.upvoteCount desc limit {$startno},3");
            $length = count($posts);
            for ($i=0; $i < $length; $i++) {
                $post_id = $posts[$i]['post_id'];
                $img = Db::query("select img_url from ym_snapshot_img where post_id = '$post_id'");
                $posts[$i]['img_urls'] = empty($img) ? [] : [$img[0]['img_url']];
                $count = Db::query("select count(*) as count from ym_snapshot_cmmt where post_id = '$post_id'");
                $posts[$i]['cmmt_count'] = $count[0]['count'];
                $upvote = Db::query("select upvote from ym_snapshot_posts_upvote where post_id = '$post_id' and user_id = '$user_id'");
                if (!isset($upvote[0]['upvote'])) {
                    $posts[$i]['upvote'] = 0;
                }else{
                    $posts[$i]['upvote'] = $upvote[0]['upvote'];
                }
                $cmmt = Db::query("select u.user_name,u.user_id,c.cmmt_content from ym_snapshot_cmmt as c left join user_info as u on u.user_id = c.user_id where c.post_id = '$post_id' limit 1");
                if(empty($cmmt)){
                    $posts[$i]['fst_cmmt'] = [[
                      'user_id' => 0,
                      'user_name' => '',
                      'cmmt_content' => ''
                    ]];
                }else{
                    $posts[$i]['fst_cmmt'] = $cmmt;
                }

            }
            return $this->returnmsg(200,"posts",$posts);
        }elseif ($type == 'new') {

            $posts = Db::query("select distinct b.user_id,b.user_name,b.user_icon,c.circle_title,c.circle_subtitle,c.circle_desc,c.circle_big_img,a.post_id,a.content,a.post_latitude,a.post_longitude,a.user_address,a.send_time,e.upvoteCount from ym_snapshot_posts as a left join user_info as b on a.user_id=b.user_id left join ym_snapshot_circle as c on a.circle_id=c.circle_id join ym_posts_upvoteCount as e on a.post_id = e.post_id where c.circle_id = '$circle_id' ORDER BY a.send_time desc limit {$startno},3");
            $length = count($posts);
            for ($i=0; $i < $length; $i++) {
                $post_id = $posts[$i]['post_id'];
                $img = Db::query("select img_url from ym_snapshot_img where post_id = '$post_id'");
                $posts[$i]['img_urls'] = empty($img) ? [] : [$img[0]['img_url']];
                $count = Db::query("select count(*) as count from ym_snapshot_cmmt where post_id = '$post_id'");
                $posts[$i]['cmmt_count'] = $count[0]['count'];

                $upvote = Db::query("select upvote from ym_snapshot_posts_upvote where post_id = '$post_id' and user_id = '$user_id'");
                if (!isset($upvote[0]['upvote'])) {
                    $posts[$i]['upvote'] = 0;
                }else{

                    $posts[$i]['upvote'] = $upvote[0]['upvote'];
                }
                $cmmt = Db::query("select u.user_name,u.user_id,c.cmmt_content from ym_snapshot_cmmt as c left join user_info as u on u.user_id = c.user_id where c.post_id = '$post_id' limit 1");
                if(empty($cmmt)){
                    $posts[$i]['fst_cmmt'] = [[
                        'user_id' => 0,
                        'user_name' => '',
                        'cmmt_content' => ''
                    ]];
                }else{
                    $posts[$i]['fst_cmmt'] = $cmmt;
                }
            }
            return $this->returnmsg(200,"posts",$posts);
        }

    }

    //显示随手拍单个话题帖子
    public function show_snapshot_post($post_id,$user_id){
     $post = Sn::getShowSnapshotPost($post_id,$user_id);
        return $this->returnmsg(200,"post",$post);
    }



//推送全部
    public function push($user_id,$message){
        $appkey = 'a3d1bad973276db7cee3f40a';
        $master_secret = 'ac5c6c6b1101643082c88913';
        $client = new JPush($appkey,$master_secret);
        $r_id = Db::query("select registration_id from ym_device_no where user_id = '$user_id'");
        $registration_id = $r_id[0]['registration_id'];

        $push = $client->push();
        $push->setPlatform('all');
        //别名
        $push->addAlias($user_id);
        //设备号
        $push->addRegistrationId($registration_id);
        // $push->iosNotification();
        $push->iosNotification($message, [
            'sound' => 'sound.cat',
            'badge' => '+1',

        ]);
        $push->options(array(
            "apns_production" => true
        ));
        // try {
        $push->send();

    }




}